<%@ page import="java.lang.reflect.Field" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
    Object condition = request.getAttribute("condition");

    StringBuilder sb = new StringBuilder();
    if(condition != null){
        Class<?> clazz = condition.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            String name = field.getName();
            field.setAccessible(true);
            Object value = field.get(condition);

            sb.append("&" + name + "=" + (value==null?"":value));
        }
    }
%>

<ul class="pagination">
    <c:if test="${currentPage > 1}">
        <li><a
                href="<c:url value="${pageUrl }"/>?currentPage=1&limit=${limit}<%=sb.toString()%>">首页</a></li>
        <li><a
                href="<c:url value="${pageUrl }"/>?currentPage=${currentPage-1}&limit=${limit}<%=sb.toString()%>">上一页</a></li>
    </c:if>
    <c:if test="${currentPage == 1}">
        <li class="disabled"><span>首页</span></li>
        <li class="disabled"><span>上一页</span></li>
    </c:if>

    <c:if test="${totalPages<= 10}">
        <c:forEach var="i" begin="1" end="${totalPages}" step="1">
            <c:if test="${currentPage == i }">
                <li class="active"><span>${i}</span></li>
            </c:if>

            <c:if test="${currentPage!= i }">
                <li><a
                        href="<c:url value="${pageUrl }"/>?currentPage=${i}&limit=${limit}<%=sb.toString()%>">${i}</a></li>
            </c:if>
        </c:forEach>
    </c:if>

    <c:if test="${totalPages > 10}">
        <c:set var="pageEnd"
               value="${(totalPages)<(currentPage +9)?(totalPages):(currentPage +9)}"
               scope="page"/>
        <c:forEach var="i" begin="${currentPage }" end="${pageEnd }" step="1">
            <c:if test="${currentPage == i }">
                <li class="active"><span>${i}</span></li>
            </c:if>

            <c:if test="${currentPage != i }">
                <li><a
                        href="<c:url value="${pageUrl }"/>?currentPage=${i}&limit=${limit}<%=sb.toString()%>">${i}</a></li>
            </c:if>
        </c:forEach>
    </c:if>

    <c:if test="${currentPage < totalPages}">
        <li><a
                href="<c:url value="${pageUrl }"/>?currentPage=${currentPage+1}&limit=${limit}<%=sb.toString()%>">下一页</a></li>
        <li><a
                href="<c:url value="${pageUrl }"/>?currentPage=${totalPages}&limit=${limit}<%=sb.toString()%>">末页</a></li>
    </c:if>
    <c:if test="${currentPage == totalPages}">
        <li class="disabled"><span>下一页</span></li>
        <li class="disabled"><span>末页</li>
    </c:if>

    <li>
        <span>共${totalPages }页</span>
    </li>
</ul>